<template>
  <div class="register-container">
    <el-form :model="form" :rules="rules" ref="registerForm" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onRegister" :loading="loading">注册</el-button>
      </el-form-item>
    </el-form>
    <div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
export default {
  data() {
    return {
      form: { username: '', password: '', nickname: '' },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false,
      errorMsg: ''
    };
  },
  methods: {
    async onRegister() {
      this.errorMsg = '';
      this.$refs.registerForm.validate(async (valid) => {
        if (!valid) return;
        this.loading = true;
        try {
          const res = await this.axios.post('/api/register', this.form);
          if (res.data.msg && res.data.msg.indexOf('注册成功') !== -1) {
            ElMessage.success('注册成功，请登录');
            this.$router.replace({ name: 'AccountLogin' });
          } else {
            this.errorMsg = res.data.msg || '注册失败';
          }
        } catch (e) {
          this.errorMsg = '网络错误或服务器异常';
        } finally {
          this.loading = false;
        }
      });
    }
  }
};
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to bottom, #6db3f2 0%, #1e69de 100%);
}
.el-form {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px 32px 32px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.12);
  min-width: 350px;
}
.error-msg {
  color: #f56c6c;
  margin-top: 12px;
  text-align: center;
}
</style> 